jQuery(document).ready(function() {
	
	jsonurl = baseurl + "postalcode/";
	postalIds = getCookie("postalIds")
    if(postalIds.length>4){
		ids = postalIds.split("|");
		provinceId = ids[0];
		cityId = ids[1];
		brgyId = ids[2];
		zipId = ids[3];
		jQuery("select#Province").populateProvince(provinceId);
		jQuery("select#City").populateTown(provinceId, cityId);
		if(provinceId==178) {
			jQuery("select#Barangay").populateBrgy(cityId, brgyId);
		} else {
			jQuery("#areabrgy").hide();
			//jQuery("#Barangay").removeClass("error");
			jQuery("#s2id_Barangay").removeClass("error");
		}
		jQuery("select#Pcode").populatePostal(cityId, brgyId, zipId);
		
	} else {
		provinceId = 0;
		cityId = 0;
		brgyId = 0;
		zipId = 0;
		
		jQuery("select#Province").populateProvince(provinceId);
	}
	//console.log("postalIds - " + provinceId + " " + cityId + " " + brgyId + " " + zipId);
		
    jQuery("select#Province").change(function () {
		var $Province = $('#Province').val();
		if ($.trim($Province).length == 0) {
			jQuery("#s2id_Province").addClass("error");	
		} else { jQuery("#s2id_Province").removeClass("error"); }
		
		jQuery("select").clearAll();
		if(jQuery(this).hasClass("disable")) {
			return;
		} else {
			province_id = jQuery(this).val();
			
			jQuery("select#City").populateTown(province_id, cityId);
		} 
	});	

    jQuery("select#City").change(function () {
		var $City = $('#City').val();
		if ($.trim($City).length == 0) {
			jQuery("#s2id_City").addClass("error");	
		} else { jQuery("#s2id_City").removeClass("error"); }
		
		jQuery("select#Barangay").clearDropdown();
		jQuery("select#Pcode").clearDropdown();
		
		if(jQuery(this).hasClass("disable")) {
			return;
		} else {
			province_id = jQuery("select#Province").val();
			city_id = jQuery("select#City").val();
			
			if(province_id==178) {
				// Metro Manila 178
				// Populate Barangay
				jQuery("#Barangay").populateBrgy(city_id, 0);
			} else {
				// Disable Barangay
				jQuery("select#Barangay").addClass("disable");
				jQuery("select#Pcode").populatePostal(city_id, 0, zipId);
			}		
		}
	});	

    jQuery("select#Barangay").change(function () {
		var $Barangay = $('#Barangay').val();
		if ($.trim($Barangay).length == 0) {
			jQuery("#s2id_Barangay").addClass("error");	
		} else { jQuery("#s2id_Barangay").removeClass("error"); }
		
		jQuery("select#Pcode").clearDropdown();
		
		if(jQuery(this).hasClass("disable")) {
			return;
		} else {
			jQuery("select#Pcode").populatePostal(jQuery("select#City").val(), jQuery("select#Barangay").val(), zipId);
		}
	});	
	
});	

/* Reusable dropdown related methods */
jQuery.fn.extend({

	// Clear specific dropdown item
	clearDropdown: function() {
		jQuery(this).select2('data', null).addClass("disable");
		jQuery("option", this).remove();
		return this;
	},
	
	// Clear all dropdown options
	clearAll: function() {
		jQuery("select#City").clearDropdown();
		jQuery("select#Barangay").clearDropdown();
		jQuery("select#Pcode").clearDropdown();
		return this;
	},
	
	populateProvince: function(provinceId) {
		//console.log("populateProvince: " + provinceId);
		jQuery.getJSON(jsonurl + "get_province",{ajax: 'true'}, function(j){
			var li = '<option></option>';

			for (var i = 0; i < j.length; i++) {
				id_ = j[i].id_;
				li += '<option value="' + id_ + '"';
				if(provinceId!=0 && provinceId==id_) li += ' selected ';
				li += '>' + j[i].name + '</option>';
			}
			//Store laman sa ul li
			jQuery("select#Province").html(li);
			select_value = jQuery("#Province").select2("val");
			jQuery("select#Province").select2('val', select_value);
			return this;
		});
	},
	
	populateTown: function(provinceId, cityId) {
		console.log("populateTown: " + provinceId + " " + cityId);
		jQuery.getJSON(jsonurl + "get_town/" + provinceId,{ajax: 'true'}, function(j){
			var li = '<option></option>';

			for (var i = 0; i < j.length; i++) {
				id_ = j[i].id_;
				li += '<option value="' + id_ + '"';
				if(cityId!=0 && cityId==id_) li += ' selected ';
				li += '>' + j[i].name + '</option>';
			} 
			//Store laman sa ul li
			if(li.length > 1) {
				jQuery("select#City").html(li).removeClass("disable");
			} else {
				jQuery("select#City").addClass("disable");
			}
			
			select_value = jQuery("#City").select2("val");
			jQuery("select#City").select2('val', select_value);			
			return this;
		});
	},
	
	populateBrgy: function(cityId, brgyId) {
		jQuery.getJSON(jsonurl + "get_barangay/" + cityId,{ajax: 'true'}, function(j){
			var li = '<option></option>';

			for (var i = 0; i < j.length; i++) {
				id_ = j[i].id_;
				li += '<option value="' + id_ + '"';
				if(brgyId!=0 && brgyId==id_) li += ' selected ';
				li += '>' + j[i].name + '</option>';				
			}
			//Store laman sa ul li
			if(li.length > 1) {
				jQuery("select#Barangay").html(li).removeClass("disable");
			} else {
				jQuery("select#Barangay").addClass("disable");
			}

			select_value = jQuery("select#Barangay").select2("val");
			jQuery("select#Barangay").select2('val', select_value);				
			return this;
		});	
	},	
	
	populatePostal: function(city_id, barangay_id, postal_id) {
		jQuery.getJSON(jsonurl + "get_pcode/" + city_id + "/" + barangay_id,{ajax: 'true'}, function(j){
			var li = '<option></option>';
			for (var i = 0; i < j.length; i++) {
				id_ = j[i].id_;
				li += '<option value="' + id_ + '"';
				if(postal_id!=0 && postal_id==id_) li += ' selected ';
				li += '>' + j[i].code_ + '</option>';				
			}
			//Store laman sa ul li
			if(li.length > 1) {
				jQuery("select#Pcode").html(li).removeClass("disable");
			} else {
				jQuery("select#Pcode").addClass("disable");
			}
			
			select_value = jQuery("select#Pcode").select2("val");
			jQuery("select#Pcode").select2('val', select_value);				
			return this;
		});		
	}
});